package com.rpframework.website.device.act.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rpframework.core.api.FileService;
import com.rpframework.module.adminbase.domain.AdminUser;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.IConvertDao;
import com.rpframework.website.device.dao.ITCommentDao;
import com.rpframework.website.device.domain.Convert;
import com.rpframework.website.device.domain.Estate;
import com.rpframework.website.device.domain.Interact;
import com.rpframework.website.device.service.EstateService;
import com.rpframework.website.device.service.IConvertService;
import com.rpframework.website.device.service.TCommentService;

@Controller
@RequestMapping("/admin/convert")
public class AdminConvertAct extends AdminAct{
	@Resource IConvertService service;
	@Resource FileService fileService;
	@Resource EstateService estateService;
	@Resource TCommentService TCommentService;
	@Resource public IConvertDao iDao;
	public @Resource ITCommentDao iCommentDao;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(
			@RequestParam(value="pager", required=false) Pager pager, 
			Map<Object, Object> model, RedirectAttributes attr,
			HttpSession session
			){
		if(pager == null) {
			pager = new Pager();
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("convert/list");
	}
	
	@RequestMapping("/edit-{id}")
	public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		Convert t = service.select(id);
		Assert.notNull(t,this.getClass().getName() + " Id 异常");
		model.put("pager", t);
		return this.add(attr, model);
	}
	
	@RequestMapping("/add")
	public String add(RedirectAttributes attr,Map<Object, Object> model){
		return this.doPackageURI("convert/edit");
	}
			
	@RequestMapping("/delete-{id}")
	public String delete(@PathVariable Integer id ,RedirectAttributes attr){
		boolean flag = service.delete(id);
		if(flag){
			setInfoMsg("删除成功！", attr);
		}else{
			setInfoMsg("删除失败！", attr);
		}
		return redirect("/admin/convert/list");
	}

	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute Convert t,
			@RequestParam(value="urlList", required=false) MultipartFile urlList,
			HttpSession session,
			RedirectAttributes attr){

		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			if(NumberUtils.isValid(t.getId())) {
				setInfoMsg("更新操作成功！", attr);
			} else {
				setInfoMsg("新增操作成功！", attr);
			}
		}else{
			setInfoMsg("操作失败！", attr);
		}
		return redirect("/admin/convert/list");
	}
}
